package org.geekbang.thinking.in.spring.ioc.dependency.source;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;

/**
 * 外部化配置作为依赖来源示例
 *
 * @author wangyh2
 * @version v1.0
 * @since 2021-10-08 14:51
 */
@Configuration
@PropertySource(value = "MATA-INF/default.properties",encoding = "UTF-8")
public class ExternalConfigurationDependencySourceDemo {

    @Value("${user.id:-1}")
    private Long id;
    @Value("${usr.name:sishen008}")
    private String name;
    @Value("${user.resource:default.properties}")
    private Resource resource;

    public static void main(String[] args) {
        // 1,创建 BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 2,注册 Configuration Class（配置类） -> Spring Bean
        applicationContext.register(ExternalConfigurationDependencySourceDemo.class);
        // 3,启动 Spring 应用上下文
        applicationContext.refresh();
        // 4,依赖查找 DependencySourceDemo Bean
        ExternalConfigurationDependencySourceDemo demo = applicationContext.getBean(ExternalConfigurationDependencySourceDemo.class);

        System.out.println("user.id = " + demo.id);
        System.out.println("user.name = " + demo.name);
        System.out.println("user.resource = " + demo.resource);

        // 5,显示地关闭 Spring 应用上下文
        applicationContext.close();
    }
}
